/**
 * 炼焦大数据智能诊断系统 - 主JavaScript文件
 */

// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 初始化Mermaid图表库
    initMermaid();
    
    // 添加滚动动画效果
    addScrollAnimations();
    
    // 添加导航栏活动状态
    setupNavigation();
});

/**
 * 初始化Mermaid图表库
 */
function initMermaid() {
    mermaid.initialize({
        startOnLoad: true,
        theme: 'default',
        flowchart: {
            useMaxWidth: true,
            htmlLabels: true,
            curve: 'basis'
        },
        securityLevel: 'loose',
        fontFamily: 'Microsoft YaHei, Segoe UI, sans-serif'
    });
}

/**
 * 添加滚动动画效果
 */
function addScrollAnimations() {
    // 获取所有需要添加动画的元素
    const elements = document.querySelectorAll('.card, .diagram-container, .chart-container');
    
    // 创建Intersection Observer
    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                entry.target.classList.add('fade-in');
                observer.unobserve(entry.target);
            }
        });
    }, {
        threshold: 0.1
    });
    
    // 观察每个元素
    elements.forEach(element => {
        observer.observe(element);
    });
}

/**
 * 设置导航栏活动状态
 */
function setupNavigation() {
    const sections = document.querySelectorAll('section[id]');
    const navLinks = document.querySelectorAll('.navbar-nav .nav-link');
    
    // 添加滚动事件监听器
    window.addEventListener('scroll', () => {
        let current = '';
        
        sections.forEach(section => {
            const sectionTop = section.offsetTop;
            const sectionHeight = section.clientHeight;
            if (pageYOffset >= (sectionTop - 200)) {
                current = section.getAttribute('id');
            }
        });
        
        navLinks.forEach(link => {
            link.classList.remove('active');
            if (link.getAttribute('href').substring(1) === current) {
                link.classList.add('active');
            }
        });
    });
}

/**
 * 创建加载动画
 * @param {string} elementId - 要添加加载动画的元素ID
 */
function createLoadingSpinner(elementId) {
    const element = document.getElementById(elementId);
    if (!element) return;
    
    element.innerHTML = `
        <div class="d-flex justify-content-center align-items-center h-100">
            <div class="spinner-border text-primary" role="status">
                <span class="visually-hidden">加载中...</span>
            </div>
        </div>
    `;
}

/**
 * 显示错误消息
 * @param {string} elementId - 要显示错误消息的元素ID
 * @param {string} message - 错误消息
 */
function showError(elementId, message) {
    const element = document.getElementById(elementId);
    if (!element) return;
    
    element.innerHTML = `
        <div class="alert alert-danger" role="alert">
            <i class="bi bi-exclamation-triangle-fill me-2"></i>
            ${message}
        </div>
    `;
} 